package com.example.application.views.studyView;

import com.example.application.bean.ClassBean;
import com.example.application.bean.StudentBean;
import com.example.application.service.IStudentService;
import com.example.application.service.impl.StudentsServiceImpl;
import com.example.application.views.main.MainView;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.crud.Crud;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.radiobutton.RadioButtonGroup;
import com.vaadin.flow.component.radiobutton.RadioGroupVariant;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import org.apache.coyote.http11.Http11AprProtocol;

import java.util.ArrayList;
import java.util.List;

@Route(value = "study",layout = MainView.class)
@PageTitle("学习vaadin")
public class StudyView extends Div {

    private IStudentService iStudentService = new StudentsServiceImpl();

    public StudyView(){

        HorizontalLayout formLayout = new HorizontalLayout();
        //文本框
        TextField nameField = new TextField();
        nameField.setLabel("学生姓名");
        nameField.setPlaceholder("请输入学生的名字");
        //单选框
        RadioButtonGroup<String> radioGroup = new RadioButtonGroup<>();
        radioGroup.setLabel("性别");
        radioGroup.setItems("男", "女");
        //设置默认被选中
        radioGroup.setValue("男");
        //下来菜单
        ComboBox<ClassBean> comboBox = new ComboBox<>();
        comboBox.setLabel("班级");
        List<ClassBean> classBeanList = new ArrayList<>();
        classBeanList.add(new ClassBean(1,"班级1"));
        classBeanList.add(new ClassBean(2,"班级2"));
        classBeanList.add(new ClassBean(3,"班级3"));
        //显示数据
        comboBox.setItems(classBeanList);
        comboBox.setValue(classBeanList.get(1));

        //创建一个按钮
        Button findBtn = new Button("查询",new Icon(VaadinIcon.SEARCH));
        findBtn.getStyle().set("margin-top","33px");
        findBtn.getStyle().set("margin-left","20px");
        formLayout.add(nameField,radioGroup,comboBox,findBtn);

        Div div = new Div();
        div.getStyle().set("border","none");
        div.getStyle().set("border-top","1px #ccc solid");
        div.getStyle().set("width","800px");
        div.getStyle().set("margin","30px 30px");
        VerticalLayout layout = new VerticalLayout(formLayout,div);

        layout.getStyle().set("margin-top","50px");
        //创建一个表格
        Grid<StudentBean> grid = new Grid<>(StudentBean.class);

        //把数据放到表格中显示
        grid.setItems(iStudentService.showAllStudents());
        //显示数据
        grid.setColumns("id","name","gender","classBean.name");
        grid.getColumnByKey("id").setHeader("编号");
        grid.getColumnByKey("name").setHeader("姓名");
        grid.getColumnByKey("gender").setHeader("性别");
        grid.getColumnByKey("classBean.name").setHeader("班级名称");

        layout.add(grid);

        this.add(layout);

    }


}

